package test;

import module.TelluriumIssuesPage;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.tellurium.test.java.TelluriumJavaTestCase;

import java.util.Map;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: sesame.yangj
 * Date: 2009-8-23
 * Time: 22:59:29
 * To change this template use File | Settings | File Templates.
 */
public class TelluriumIssuesPageJaveTestCase extends TelluriumJavaTestCase {
    private static TelluriumIssuesPage tip;

    @BeforeClass
    public static void initUi() {
        tip = new TelluriumIssuesPage();
        tip.defineUi();
        tip.disableSelectorCache();
    }

//      @Test
//      public void testGetIssueType(){
//          connectUrl("http://code.google.com/p/aost/issues/list");
//          String[] issues=tip.getIssueTypes();
//          assertNotNull(issues);
//          assertTrue(issues[2].contains("Open issues"));
//          tip.selectIssueType(issues[2]);
//          tip.searchIssue("Alter");
//      }
//
//     @Test
//    public void testAdvanceSearch(){
//         connectUrl("http://code.google.com/p/aost/issues/advsearch");
//         String[] issues=tip.getAdvancedIssueTypes();
//         assertNotNull(issues);
//         assertTrue(issues[1].contains("All issues"));
//         tip.selectIssueType(issues[1]);
//
//         tip.advancedSearchIssue(issues[1],"table",null,null,null,null,null,null,null);
//     }
//
//     @Test
//    public void testAdvancedSearchTips(){
//         connectUrl("http://code.google.com/p/aost/issues/advsearch");
//         tip.clickMoreSearchTips();
//     }
//     @Test
//    public void testIssueData(){
//         connectUrl("http://code.google.com/p/aost/issues/list");
//         int mcolumn=tip.getTableHeaderNum();
//         assertEquals(10,mcolumn);
//         List<String> list=tip.getHeaderNames();
//         assertNotNull(list);
//         assertTrue(Helper.include(list,"Status"));
//         list=tip.getDataForColumn(7);
//         assertNotNull(list);
//         assertFalse(list.isEmpty());
//         assertTrue(Helper.include(list,"John.Jian.Fang"));
//     }
//    @Test
//    public void  testClickIssueResult(){
//        connectUrl("http://code.google.com/p/aost/issues/list");
//        tip.clickTable(1,2);
//        connectUrl("http://code.google.com/p/aost/issues/list");
//        tip.clickTable(1,3);

    //    }

//    @Test
//    public void testClickHeader() {
//        connectUrl("http://code.google.com/p/aost/issues/list");
//        tip.clickOnTabHeader(2);
//        tip.clickOnTabHeader(3);
//        tip.clickOnTabHeader(4);
//        tip.clickOnTabHeader(5);
//        tip.clickOnTabHeader(6);
//        tip.clickOnTabHeader(7);
//        tip.clickOnTabHeader(9);
//    }
//    @Test
//    public void testIdMenu(){
//        connectUrl("http://code.google.com/p/aost/issues/list");
//        tip.clickOnTabHeader(2);
//        tip.mouseMoveIdMenu();
//        tip.clickIdMenuSortDown();
//        tip.clickIdMenuSortUp();
//    }

//    @Test
//    public void testSelectColumnMenu() {
//        connectUrl("http://code.google.com/p/aost/issues/list");
//        tip.toggleIdColumn("ID");
//        tip.toggleIdColumn("Closed");
//    }

    @Test
    public void testSelectDataLayout() {
        connectUrl("http://code.google.com/p/aost/issues/list");
        tip.selectDataLayout("Grid");
        tip.selectDataLayout("List");
    }

    //
    @Test
    public void testGetCellCount() {
        tip.useJQuerySelector();
        int count = tip.getTableCellCount();
        assertTrue(count > 0);
        System.out.println("Cell size:" + count);
        String[] details = tip.getAllText();
        assertNotNull(false);
        assertEquals(details.length, count);
    }

    @Test
    public void testSearchIssueType() {
        tip.useJQuerySelector();
        tip.enableSelectorCache();
        tip.setCacheMaxSize(10);
        String[] issues = tip.getIssueTypes();
        tip.selectIssueType(issues[2]);
        tip.searchIssue("Alter");
        showCacheUsage();
    }

    @Test
    public void testDump() {
        tip.disableJQuerySelector();
        tip.dump("issueAdvancedSearch");

        tip.useJQuerySelector();
        tip.disableSelectorCache();
        tip.dump("issueAdvancedSearch");

        tip.useJQuerySelector();
        tip.enableSelectorCache();
        tip.dump("issueAdvancedSearch");
    }

    protected void showCacheUsage() {
        int size = tip.getCacheSize();
        int maxSize = tip.getCacheMaxSize();
        System.out.println("Cache Size: " + size + ", Cache Max Size: " + maxSize);
        System.out.println("Cache Usage: ");
        Map<String, Long> usages = tip.getCacheUsage();
        Set<String> keys = usages.keySet();
        for (String key : keys) {
            System.out.println("UID: " + key + ", Count: " + usages.get(key));
        }
    }
}
